﻿using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Dapper.Sharding
{
    public class TextJsonOptions
    {

        static TextJsonOptions()
        {
#if CORE6
            JsonOptions.Converters.Add(new DateOnlyConverter());
            JsonOptions.Converters.Add(new TimeOnlyConverter());

            JsonOptionsMs.Converters.Add(new DateOnlyConverter());
            JsonOptionsMs.Converters.Add(new TimeOnlyConverter());

            JsonOptionsNs.Converters.Add(new DateOnlyConverter());
            JsonOptionsNs.Converters.Add(new TimeOnlyConverter());

            JsonOptionsWeb.Converters.Add(new DateOnlyConverter());
            JsonOptionsWeb.Converters.Add(new TimeOnlyConverter());

            JsonOptionsComment.Converters.Add(new DateOnlyConverter());
            JsonOptionsComment.Converters.Add(new TimeOnlyConverter());

            JsonOptionsIndented.Converters.Add(new DateOnlyConverter());
            JsonOptionsIndented.Converters.Add(new TimeOnlyConverter());
#endif
            JsonOptions.Converters.Add(new DateTimeConverter());
            JsonOptions.Converters.Add(new DateTimeOffsetConverter());

            JsonOptionsMs.Converters.Add(new DateTimeMsConverter());
            JsonOptionsMs.Converters.Add(new DateTimeOffsetMsConverter());

            JsonOptionsNs.Converters.Add(new DateTimeNsConverter());
            JsonOptionsNs.Converters.Add(new DateTimeOffsetNsConverter());

            JsonOptionsWeb.Converters.Add(new DateTimeConverter());
            JsonOptionsWeb.Converters.Add(new DateTimeOffsetConverter());

            JsonOptionsComment.Converters.Add(new DateTimeConverter());
            JsonOptionsComment.Converters.Add(new DateTimeOffsetConverter());

            JsonOptionsIndented.Converters.Add(new DateTimeConverter());
            JsonOptionsIndented.Converters.Add(new DateTimeOffsetConverter());
        }

        public static readonly JsonSerializerOptions JsonOptions = new()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //序列化属性不转小写
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//不编码中文和html
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true, //反序列化忽略大小写
            PropertyNamingPolicy = null,
            DictionaryKeyPolicy = null
        };

        public static readonly JsonSerializerOptions JsonOptionsMs = new()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //序列化属性不转小写
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//不编码中文和html
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true, //反序列化忽略大小写
            PropertyNamingPolicy = null,
            DictionaryKeyPolicy = null
        };

        public static readonly JsonSerializerOptions JsonOptionsNs = new()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //序列化属性不转小写
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//不编码中文和html
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true, //反序列化忽略大小写
            PropertyNamingPolicy = null,
            DictionaryKeyPolicy = null
        };

        /// <summary>
        /// 驼峰式序列化
        /// </summary>
        public static JsonSerializerOptions JsonOptionsWeb { get; set; } = new()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //null序列化属性名称不变,小写JsonNamingPolicy.CamelCase
            DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, //不编码中文和html标签
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true //true反序列化忽略大小写(开销大)
        };

        public static readonly JsonSerializerOptions JsonOptionsComment = new()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //序列化属性不转小写
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//不编码中文和html
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true, //反序列化忽略大小写
            PropertyNamingPolicy = null,
            DictionaryKeyPolicy = null,
            AllowTrailingCommas = true, //允许尾部逗号
            ReadCommentHandling = JsonCommentHandling.Skip //跳过注释
        };

        public static readonly JsonSerializerOptions JsonOptionsIndented = new()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //序列化属性不转小写
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//不编码中文和html
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true, //反序列化忽略大小写
            PropertyNamingPolicy = null,
            DictionaryKeyPolicy = null,
            AllowTrailingCommas = true, //允许尾部逗号
            ReadCommentHandling = JsonCommentHandling.Skip, //跳过注释
            WriteIndented = true
        };
    }
}